import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * 模板编辑器对话框
 */
public class TemplateEditorDialog extends JDialog {
    private BillTemplate template;
    private JPanel fieldPanel;
    private List<FieldPanel> fieldPanels = new ArrayList<>();
    private JTextField nameField;
    private JTextField widthField;
    private JTextField heightField;

    public TemplateEditorDialog(Frame owner, BillTemplate template) {
        super(owner, "票据模板编辑器", true);
        this.template = template != null ? template : new BillTemplate();
        initComponents();
        setSize(700, 500);
        setLocationRelativeTo(owner);
    }

    private void initComponents() {
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));

        // 模板信息面板
        JPanel infoPanel = new JPanel(new GridLayout(3, 2, 5, 5));
        infoPanel.add(new JLabel("模板名称:"));
        nameField = new JTextField(template.getTemplateName());
        infoPanel.add(nameField);

        infoPanel.add(new JLabel("宽度(mm):"));
        widthField = new JTextField(String.valueOf(template.getTemplateWidth()));
        infoPanel.add(widthField);

        infoPanel.add(new JLabel("高度(mm):"));
        heightField = new JTextField(String.valueOf(template.getTemplateHeight()));
        infoPanel.add(heightField);

        mainPanel.add(infoPanel, BorderLayout.NORTH);

        // 字段列表面板
        fieldPanel = new JPanel();
        fieldPanel.setLayout(new BoxLayout(fieldPanel, BoxLayout.Y_AXIS));

        JScrollPane scrollPane = new JScrollPane(fieldPanel);
        mainPanel.add(scrollPane, BorderLayout.CENTER);

        // 加载现有字段
        for (BillTemplate.FieldInfo field : template.getFields()) {
            addFieldPanel(field);
        }

        // 添加字段按钮
        JButton addFieldButton = new JButton("添加字段");
        addFieldButton.addActionListener(this::addFieldAction);


        // 底部按钮
        JPanel bottomPanel = new JPanel();
        JButton saveButton = new JButton("保存");
        JButton cancelButton = new JButton("取消");

        saveButton.addActionListener(this::saveAction);
        cancelButton.addActionListener(e -> dispose());
        bottomPanel.add(addFieldButton);
        bottomPanel.add(saveButton);
        bottomPanel.add(cancelButton);

        mainPanel.add(bottomPanel, BorderLayout.SOUTH);

        setContentPane(mainPanel);
    }

    private void addFieldPanel(BillTemplate.FieldInfo field) {
        FieldPanel panel = new FieldPanel(field);
        fieldPanels.add(panel);
        fieldPanel.add(panel);
        fieldPanel.revalidate();
        fieldPanel.repaint();
    }

    private void addFieldAction(ActionEvent e) {
        addFieldPanel(null);
    }

    private void saveAction(ActionEvent e) {
        try {
            template.setTemplateName(nameField.getText());
            template.setTemplateWidth(Float.parseFloat(widthField.getText()));
            template.setTemplateHeight(Float.parseFloat(heightField.getText()));

            List<BillTemplate.FieldInfo> fields = new ArrayList<>();
            for (FieldPanel panel : fieldPanels) {
                BillTemplate.FieldInfo field = panel.getFieldInfo();
                if (field != null) {
                    fields.add(field);
                }
            }

            template.setFields(fields);
            dispose();
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "请输入有效的数字", "输入错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    public BillTemplate getTemplate() {
        return template;
    }

    /**
     * 字段编辑面板内部类
     */
    private class FieldPanel extends JPanel {
        private JTextField nameField;
        private JTextField xField;
        private JTextField yField;
        private JTextField widthField;
        private JTextField heightField;
        private JTextField fontSizeField;

        public FieldPanel(BillTemplate.FieldInfo field) {
            setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
            setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));

            nameField = new JTextField(field != null ? field.getName() : "字段名", 8);
            xField = new JTextField(field != null ? String.valueOf(field.getX()) : "0", 3);
            yField = new JTextField(field != null ? String.valueOf(field.getY()) : "0", 3);
            widthField = new JTextField(field != null ? String.valueOf(field.getWidth()) : "100", 3);
            heightField = new JTextField(field != null ? String.valueOf(field.getHeight()) : "20", 3);
            fontSizeField = new JTextField(field != null ? String.valueOf(field.getFontSize()) : "10", 3);

            add(new JLabel("名称:"));
            add(nameField);
            add(new JLabel("X:"));
            add(xField);
            add(new JLabel("Y:"));
            add(yField);
            add(new JLabel("宽度:"));
            add(widthField);
            add(new JLabel("高度:"));
            add(heightField);
            add(new JLabel("字体:"));
            add(fontSizeField);

            JButton removeButton = new JButton("删除");
            removeButton.addActionListener(e -> {
                fieldPanel.remove(this);
                fieldPanels.remove(this);
                fieldPanel.revalidate();
                fieldPanel.repaint();
            });

            add(removeButton);
        }

        public BillTemplate.FieldInfo getFieldInfo() {
            try {
                BillTemplate.FieldInfo field = new BillTemplate.FieldInfo();
                field.setName(nameField.getText());
                field.setX(Float.parseFloat(xField.getText()));
                field.setY(Float.parseFloat(yField.getText()));
                field.setWidth(Float.parseFloat(widthField.getText()));
                field.setHeight(Float.parseFloat(heightField.getText()));
                field.setFontSize(Float.parseFloat(fontSizeField.getText()));
                return field;
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(TemplateEditorDialog.this, "字段输入格式错误", "输入错误", JOptionPane.ERROR_MESSAGE);
                return null;
            }
        }
    }
}
